package com.hyf.snackbartoast.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyf.snackbartoast.R;

/**
 * Created by Administrator on 2017/11/16.
 */

public class SnackTopToast {
    private static final int HIDE_SNACK_TOAST = 0;


    private Context mContext;

    private WindowManager mWm;
    private WindowManager.LayoutParams mParams;
    private View mView;

    private LayoutInflater mLayoutInflater;

    // 视图
    private FrameLayout contentView;
    private ImageView mImageView;
    private TextView mTextView;

    /**
     * 逻辑
     */
    private int backgroundColor = Color.WHITE;
    private int iconRes;
    private int iconColor = Color.BLACK;
    private String title = "";
    private int titleColor = Color.BLACK;
    // 显示时间
    private long showTime = 2000;


    public SnackTopToast(Context context, boolean translucent) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        init(translucent);
    }

    private void init(boolean translucent) {
        mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mParams = new WindowManager.LayoutParams();
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.width = WindowManager.LayoutParams.MATCH_PARENT;

        mParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mParams.setTitle("Toast");
        // 设置吐司窗体的标识
        mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        // 透明，不透明会出现重叠效果
        mParams.format = PixelFormat.TRANSLUCENT;
        // 位置属性
        mParams.gravity = Gravity.TOP + Gravity.LEFT;  // 左上
        //出现动画
        mParams.windowAnimations = R.style.Animation_Snack_Toast;

        // 初始化吐司窗口布局
        //mView = View.inflate(mContext, R.layout.layout_snack_toast, null);
        mView = mLayoutInflater.inflate(R.layout.layout_snack_toast, null);

        contentView = (FrameLayout) mView.findViewById(R.id.hyf_fl_snack_toast_view);
        mImageView = (ImageView) mView.findViewById(R.id.hyf_iv_snack_toast);
        mTextView = mView.findViewById(R.id.hyf_tv_snack_toast);
        // 设置margintop
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && translucent) {
            LinearLayout parent = (LinearLayout) contentView.getChildAt(0);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) parent.getLayoutParams();
            params.topMargin = getStatusBarHeight(mContext);
            parent.setLayoutParams(params);
        }
    }

    private Handler mHandler;

    public SnackTopToast setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }

    public SnackTopToast setIconRes(int iconRes) {
        this.iconRes = iconRes;
        return this;
    }

    public SnackTopToast setTitle(String title) {
        this.title = title;
        return this;
    }

    public SnackTopToast setTitleColor(int titleColor) {
        this.titleColor = titleColor;
        return this;
    }

    public SnackTopToast setShowTime(long showTime) {
        this.showTime = showTime;
        return this;
    }

    public SnackTopToast setIconColor(int iconColor) {
        this.iconColor = iconColor;
        return this;
    }

    public void show() {
        contentView.setBackgroundColor(backgroundColor);
        if (iconRes != 0) {
            Drawable drawable = mContext.getResources().getDrawable(iconRes);
            drawable.setColorFilter(iconColor, PorterDuff.Mode.SRC_IN);
            mImageView.setImageDrawable(drawable);
        }
        mImageView.setVisibility(iconRes == 0 ? View.GONE : View.VISIBLE);
        mTextView.setText(title);
        mTextView.setTextColor(titleColor);

        if (mHandler == null) {
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == HIDE_SNACK_TOAST) {
                        hide();
                    }
                }
            };
        }
        mHandler.removeMessages(HIDE_SNACK_TOAST);
        mHandler.sendEmptyMessageDelayed(HIDE_SNACK_TOAST, showTime);

        if (mView != null) {
            if (mView.getParent() != null) {
                mWm.removeView(mView);
            }
        }

        contentView.setOnClickListener(mClickListener);

        // 添加到窗体管理器中才能显示出来
        mWm.addView(mView, mParams);
    }

    private void hide() {
        if (mView != null) {
            if (mView.getParent() != null) {
                mWm.removeView(mView);
                mHandler = null;
            }
        }
    }

    // 点击contentview让view消失的点击事件
    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mHandler == null) return;
            mHandler.removeMessages(HIDE_SNACK_TOAST);
            hide();
        }
    };

    /**
     * 获取状态栏的高度
     *
     * @param context
     * @return
     */
    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
